###############################################################
### Replication Code: Analysis of Pilot Data                ###
### Title: Can Conservatives Be Persuaded to Support UBI?   ###
### Author: Eddy S. F. Yeung                                ###
### Version: September 6, 2022                              ###
###############################################################

# Set-up ----
## Clean the R environment and set the working directory
rm(list = ls())
setwd("~/Desktop/UBI/PB_replication") # change to your own directory

## Load the required packages
library(tidyverse) # version 1.3.1
library(extrafont) # version 0.17

## Import the pilot data
df <- read.csv("pilot.csv")
df <- df[-c(1, 2), ]

# Recode demographic variables ----
## Age
df$age <- as.numeric(df$age) + 17

## Self-identified ideology
df$conservative <- ifelse(df$ideo == "5" | df$ideo == "6" | df$ideo == "7", 1,
                          ifelse(df$ideo == "1" | df$ideo == "2" | 
                                 df$ideo == "3" | df$ideo == "4", 0, NA))

## Sex (1 = female)
df$female <- ifelse(df$sex == "2", 1, 
                    ifelse(df$sex == "1", 0, NA))

## Marital status (1 = married)
df$married <- ifelse(df$marital == "2", 1, 
                     ifelse(is.na(df$marital) == F, 0, NA))

## Education (0 = less than high school; 7 = doctorate degree)
df$educ <- as.numeric(df$educ) - 1

## Income (0 = less than $10,000; 13 = $150,000 or more)
df$income <- as.numeric(df$income) - 1

## Race
df$white <- ifelse(df$race == "1", 1,                        # (1 = White)
                   ifelse(df$race == "2" | df$race == "3" | 
                          df$race == "4" | df$race == "5" |
                          df$race == "6" | df$race == "7", 0, NA))
df$black <- ifelse(df$race == "2", 1,                        # (1 = Black)
                   ifelse(df$race == "1" | df$race == "3" | 
                            df$race == "4" | df$race == "5" |
                            df$race == "6" | df$race == "7", 0, NA))

## Employment status
df$employ <- ifelse(df$employ == "1" | df$employ == "2", 0,  # (0 = employed)
                    ifelse(df$employ == "3", 1,              # (1 = not employed)
                    ifelse(df$employ == "4", 2, NA)))        # (2 = retired)
df$employ <- as.factor(df$employ)

## Party identification (0 = strong Democrat; 6 = strong Republican)
df$pid <- ifelse(df$partyid.2d == "1", 0,
                 ifelse(df$partyid.2d == "2", 1,
                 ifelse(df$partyid.2i == "2", 2,
                 ifelse(df$partyid.2i == "3", 3,
                 ifelse(df$partyid.2i == "1", 4,
                 ifelse(df$partyid.2r == "2", 5,
                 ifelse(df$partyid.2r == "1", 6, NA)))))))

# Recode other variables ----
## Support for UBI (0 = strongly against; 6 = strongly in favor)
df$support <- as.numeric(df$support) - 1

## Agreement with UBI as an outcome-equalizing policy (0 = strongly disagree; 4 = strongly agree)
df$check.opp <- as.numeric(df$check.opp) - 1

## Agreement with UBI as a government-limiting policy (0 = strongly disagree; 4 = strongly agree)
df$check.gov <- as.numeric(df$check.gov) - 1

## Association of UBI with political parties (1 = yes)
df$asso1 <- ifelse(df$party1a == "1", 1, 
                   ifelse(df$party1a == "2", 0, NA))

## Association of UBI with Democratic Party (1 = yes)
df$asso2 <- ifelse(df$party1b == "1", 1,
                   ifelse(df$party1b == "2" | df$party1b == "3", 0, NA))

## Perceived likelihood of partisan support
df$party.supp <- ifelse(df$party2 == "1", 1,              # (1 = Democrat)
                        ifelse(df$party2 == "2", 2,       # (2 = Republican)
                        ifelse(df$party2 == "3", 3,       # (3 = equally likely)
                               4)))                       # (4 = DK)

## Association of UBI with specific politicians (1 = yes)
df$asso3 <- ifelse(df$politician1 == "1", 1,
                   ifelse(df$politician1 == "2", 0, NA))

## Heard of UBI before (1 = yes)
df$heard <- ifelse(df$heard == "1", 1,
                   ifelse(df$heard == "2", 0, NA))

# Analysis ----
## Calculate the SD of the dependent variable among conservatives
sd(df[which(df$conservative == 1), ]$support)

## Calculate the average support for UBI across the two pilot groups
## (testing the wording between "equality of opportunity" and "level playing field")
df.ubi.support <- df %>%
  group_by(group, conservative) %>% 
  mutate(count = n()) %>% 
  group_by(group, conservative, count) %>% 
  summarize_at(vars(support, check.opp, check.gov), list(support = mean))
with(df, t.test(support[group == 1], support[group == 2], 
                alternative = "two.sided", var.equal = T)) # simple t-test

## Analyze how many people associate UBI with a political party
sum(df$asso1) # full sample
sum(df[which(df$conservative == 1), ]$asso1) # conservatives only

## Analyze how many people associate UBI with Democratic Party
sum(df$asso2, na.rm = T) # full sample
sum(df[which(df$conservative == 1), ]$asso2, na.rm = T) # conservatives only

## Analyze how many people associate UBI with politicians
sum(df$asso3)

## Analyze how many people have heard of UBI before
sum(df$heard)

## Analyze whether conservatives naturally associate UBI with Democratic Party
table(df[which(df$conservative == 1), ]$party.supp)